<!--用户添加-->
<template>
    <div class="user-add-root">
        <el-dialog
                title="添加系统用户"
                :visible.sync="dialogVisible"
                width="30%"
                :close="close"
                :before-close="close">

            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="用户名称">
                    <el-input v-model="form.userName" clearable></el-input>
                </el-form-item>
                <el-form-item label="账号">
                    <el-input v-model="form.account" clearable></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.pwd" clearable></el-input>
                </el-form-item>
                <el-form-item label="角色">
                    <el-select v-model="form.roleId" clearable placeholder="请选择" @change="change">
                        <el-option v-for="item in roleList" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close">取 消</el-button>
                <el-button type="primary" @click="addUser">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import API from '@/api/api'
import {RoleEnum} from "../../common/RoleEnum";
export default {
    name: "UserAdd",
    props: ["roleList"],
    data() {
        return {
            // 对话框标志位
            dialogVisible: true,
            form: {
                userRoleDTOSet: null,
                roleList: null
            },
        }
    },
    mounted() {

    },
    methods: {
        close() {
            this.$emit("close");
        },
        // 角色选择框
        change() {
          if (this.form.roleId === RoleEnum.ADMIN && !this.$hasAdmin) {
              this.form.roleId = '';
              this.$message.closeAll();
              this.$message.warning({
                  showClose: true,
                  message: "你没有添加管理员的权限!"
              });
          }
        },
        // 添加用户
        addUser() {
            let url = API.sysUser.add;
            let param = JSON.parse(JSON.stringify(this.form));
            if (param.roleId) {
                param.userRoleDTOSet = [];
                param.userRoleDTOSet.push({roleId: param.roleId});
                param.roleList = param.userRoleDTOSet
            }
            this.$post(url, param).then( res => {
                let data = res.data;
                if ( data.success ) {
                    this.$message.closeAll();
                    this.$message.success({
                        showClose: true,
                        message: data.msg
                    });
                    this.close();
                } else {
                    this.$message.closeAll();
                    this.$message.error({
                        showClose: true,
                        message: data.msg
                    })
                }
            }).catch( err => {
                this.$message.closeAll();
                this.$message.error({
                    showClose: true,
                    message: "请求失败!"
                })
            })
        }
    }
}
</script>

<style scoped>

</style>
